package org.niu.tree;

import java.util.LinkedList;
import java.util.Queue;

public class MaxDepth {
    public int maxDepth (TreeNode root) {
        // write code here
        if(root == null) return 0;
        Queue<TreeNode> q = new LinkedList<>();
        q.add(root);
        int cnt = 0;
        while (!q.isEmpty()){
            int size = q.size();
            for (int i = 0; i < size; i++) {
                TreeNode poll = q.poll();
                if(poll.left!=null) q.add(poll.left);
                if(poll.right!=null) q.add(poll.right);
            }
            cnt++;
        }
        return cnt;
    }
}
